import {
  Body,
  Controller,
  Get,
  Inject,
  Post,
  Provide,
  Query,
  ALL,
  Put,
  Param,
  Del,
} from '@midwayjs/decorator';
import { BillDTO } from '../dto/bill';
import { BillService } from '../service/bill';

@Provide()
@Controller('/bill')
export class BillController {
  @Inject()
  billService: BillService;

  @Post('/', { description: '新建' })
  async create(@Body(ALL) data: BillDTO) {
    return await this.billService.create(data.toEntity());
  }

  @Put('/', { description: '编辑' })
  async edit(@Body(ALL) data: BillDTO) {
    const bill = await this.billService.getById(data.id);
    // update
    return await this.billService.edit(bill);
  }

  @Del('/:id', { description: '删除' })
  async remove(@Param('id') id: string) {
    const bill = await this.billService.getById(id);
    await this.billService.remove(bill);
  }

  @Get('/:id', { description: '根据id查询' })
  async getById(@Param('id') id: string) {
    return await this.billService.getById(id);
  }

  @Get('/page', { description: '分页查询' })
  async page(@Query('page') page: number = 1, @Query('size') size: number = 10) {
    return await this.billService.page(page, size);
  }

  @Get('/list', { description: '查询全部' })
  async list(@Query('date') date: string) {
    return await this.billService.tableList(date);
  }
}